package com.company.blockqueue;

import com.company.cache.MyFunc0;
import com.company.thread.ReentrantLockDemo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description: some desc
 * @author: ruoan
 * @date: 2020/12/11 18:40
 */

class MyResource {
    // 共享的控制通知变量
    private volatile boolean flag = true;

    public MyResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
        System.out.println(blockingQueue.getClass().getName());
    }

    // 阻塞队列
    private BlockingQueue<String> blockingQueue = null;
    // 原子整型
    private AtomicInteger atomicInteger = new AtomicInteger();

    /**
     * 生产方法
     */
    public void myProd() throws InterruptedException {
        String data = "";
        boolean retVal;
        while (flag) {
            data = atomicInteger.getAndIncrement()+"";
            retVal = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);
            if (retVal) {
                System.out.println(Thread.currentThread().getName()+"\t 生产ok");
            }else{
                System.out.println(Thread.currentThread().getName()+"\t 生产err");
            }
            try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }


        }
        // 如果不处于循环，则是因为 flag = false
        System.out.println(Thread.currentThread().getName()+"生产过程结束");
    }



    /**
     * 消费方法
     */
    public void myConsume() throws InterruptedException {
        String data;

        while (flag) {
            data = blockingQueue.poll( 2L, TimeUnit.SECONDS);





            if (null == data || data.equalsIgnoreCase("")) {
                flag = false;
                System.out.println(Thread.currentThread().getName() + "\t 消费退出");
                System.out.println();
                System.out.println();
                return;
            }else{
                System.out.println(Thread.currentThread().getName()+"\t 消费ok");
            }
            //System.out.println(Thread.currentThread().getName() + "\t 消费成功");



        }
        // 如果不处于循环，则是因为 flag = false
        System.out.println(Thread.currentThread().getName()+"生产过程结束");
    }


    /**
     * 结束生产和消费的方法
     */
    public void end(){
        flag = false;
    }


}

public class BlockingQueueDemo2 {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(8);
        MyResource myResource = new MyResource(blockingQueue);
        new Thread(() -> {
            try {
                myResource.myProd();  // 生产
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();

        new Thread(() -> {
            try {
                myResource.myConsume();  // 消费
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t2").start();


        try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); }

        myResource.end();


    }

}
